define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
	if(params.type == '2'){
		$scope.form.type = "'1','2'";
	}else if(params.type == '3'){
		$scope.form.type = "'1','2'";
	}
	
	$scope.form.fk_organization = params.fk_organization;

	//查询父类组织
	httpService.post(app.urls.organization_selectByType,$scope.form,function(data){
		if("0000" === data.code){
			$scope.forgs = data.data;
			$scope.findOrg();
		}else{
			alert(data.msg);
		}
	});

	$scope.findOrg = function(){
	//按id查询部门
	httpService.post(app.urls.organization_selectById,$scope.form,function(data){
		if("0000" === data.code){
			$scope.form.name = data.data.name;
			$scope.findFatherOrg();
		}else{
			alert(data.msg);
		}
	});
	
	}
	
	$scope.findFatherOrg = function(){
		//按id查询主部门
		httpService.post(app.urls.organization_selectByFather,$scope.form,function(data){
			if("0000" === data.code){
				$scope.form.father_id = data.data.id+','+data.data.type;
			}else{
				alert(data.msg);
			}
		});
	}
	$scope.saveForm = function(){
		if($scope.form.father_id.split(',')[1] == '1'){
				$scope.form.type = '2';
		}else if($scope.form.father_id.split(',')[1] == '2'){
				$scope.form.type = '3';
		}
		$scope.form.father_id = $scope.form.father_id.split(',')[0];
		if($scope.formCheck()){
			httpService.post(app.urls.organization_selectIfExit,$scope.form,function(data){
				if("0000" === data.code){
					if(data.data.length == 0){
						httpService.post(app.urls.organization_update,$scope.form,function(data){
							if("0000" === data.code){
								var data = {id:params.fk_organization,type:$scope.form.type};
								eventBusService.publish("content.pageRole.reload",data);
								eventBusService.publish("content.page.reload",data);
								eventBusService.publish("content.name.reload",$scope.form.name);
								eventBusService.publish("modal.page.close",{});
				    		}else{
				    			alert(data.msg);
				    		}
						});
					}else{
						alert("该部门已存在");
					}
	    		}else{
	    			alert(data.msg);
	    		}
			});
		}
	}
}}});